package OrderController

import (
	"QingDai-Gin/api/entities"
	"QingDai-Gin/api/models"
	"QingDai-Gin/service/OrderService"
	"QingDai-Gin/service/ShopService"
	"QingDai-Gin/utils/JwtUtil"
	"github.com/gin-gonic/gin"
	"time"
)

// GetOrderByJwt
// @Tags 订单
// @Summary 根据jwt用户获取自己订单
// @Router /order/jwt [get]
func GetOrderByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	orders, _ := OrderService.GetOrdersByP(map[string]any{"buyer_id": UserId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据jwt用户获取自己订单成功", Data: orders})
}

// CreateOrderByJwt
// @Tags 订单
// @Summary 根据jwt创建新订单
// @Router /order/jwt [post]
func CreateOrderByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	deliverAddressId, _ := context.Get("deliverAddressId")
	shopId, _ := context.Get("shopId")
	commodityIdList, _ := context.Get("commodityIdList")
	commodityNumberList, _ := context.Get("commodityNumberList")
	userCouponIdList, _ := context.Get("userCouponIdList")
	commodityPaymentList, _ := context.Get("commodityPaymentList")
	totalPayment, _ := context.Get("totalPayment")
	now := time.Now()
	timeNow := now.Format("2006-01-02 15:04:05")
	order := models.Order{
		DeliverAddressId:     deliverAddressId.(int),
		ShopId:               shopId.(int),
		BuyerId:              UserId,
		CommodityIdList:      commodityIdList.(string),
		CommodityNumberList:  commodityNumberList.(string),
		CreationTime:         timeNow, //
		PaymentTime:          "",
		SendTime:             "",
		EvaluateTime:         "",
		TrackingNumber:       "", //
		UseCouponIdList:      userCouponIdList.(string),
		CommodityPaymentList: commodityPaymentList.(string),
		TotalPayment:         totalPayment.(float64),
	}
	OrderService.CreateOrder(order)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据jwt创建新订单成功", Data: nil})
}

// UpdateOrderByIdAndJwt
// @Tags 订单
// @Summary 根据id及jwt更新订单
// @Router /order/jwt/id [put]
func UpdateOrderByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	orderGet, _ := OrderService.GetOrderById(id.(int))
	if orderGet.BuyerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备编辑该订单权限", Data: nil})
	}
	deliverAddressId, _ := context.Get("deliverAddressId")
	shopId, _ := context.Get("shopId")
	commodityIdList, _ := context.Get("commodityIdList")
	commodityNumberList, _ := context.Get("commodityNumberList")
	userCouponIdList, _ := context.Get("userCouponIdList")
	commodityPaymentList, _ := context.Get("commodityPaymentList")
	totalPayment, _ := context.Get("totalPayment")
	order := models.Order{
		ID:                   id.(int),
		DeliverAddressId:     deliverAddressId.(int),
		ShopId:               shopId.(int),
		BuyerId:              UserId,
		CommodityIdList:      commodityIdList.(string),
		CommodityNumberList:  commodityNumberList.(string),
		CreationTime:         "",
		PaymentTime:          "",
		SendTime:             "",
		EvaluateTime:         "",
		TrackingNumber:       "",
		UseCouponIdList:      userCouponIdList.(string),
		CommodityPaymentList: commodityPaymentList.(string),
		TotalPayment:         totalPayment.(float64),
	}
	OrderService.UpdateOrder(order)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及jwt更新订单成功", Data: order})
}

// GetOrderByIdAndJwt
// @Tags 订单
// @Summary 根据id及jwt获取订单
// @Router /order/jwt/id [get]
func GetOrderByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	orderGet, _ := OrderService.GetOrderById(id.(int))
	if orderGet.BuyerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备查询订单权限", Data: nil})
	}
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及jwt获取订单成功", Data: orderGet})
}

// DeleteOrderByIdAndJwt
// @Tags 订单
// @Summary 根据id及jwt删除订单
// @Router /order/jwt/id [delete]
func DeleteOrderByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	orderGet, _ := OrderService.GetOrderById(id.(int))
	if orderGet.BuyerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备删除该订单权限", Data: nil})
	}
	OrderService.DeleteOrder(orderGet)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及jwt删除订单成功", Data: nil})
}

// GetOrderByShopIdAndJwt
// @Tags 订单
// @Summary 根据shopId获取订单
// @Router /order/jwt/shopId [get]
func GetOrderByShopIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	shopId, _ := context.Get("shopId")
	shop, _ := ShopService.GetShopById(shopId.(int))
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备查询该商店订单权限", Data: nil})
	}
	OrderService.GetOrdersByP(map[string]any{"shop_id": shopId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据shopId获取订单成功", Data: shopId})
}

// ChangePayTimeByIdAndJwt
// @Tags 订单
// @Summary 根据id及jwt支付
// @Router /order/jwt/pay/id [get]
func ChangePayTimeByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	orderGet, _ := OrderService.GetOrderById(id.(int))
	if orderGet.BuyerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备支付该订单权限", Data: nil})
	}
	now := time.Now()
	timeNow := now.Format("2006-01-02 15:04:05")
	order := models.Order{
		ID:          id.(int),
		PaymentTime: timeNow,
	}
	OrderService.UpdateOrder(order)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及jwt支付成功", Data: nil})
}

// DeliverOrderByIdAndJwt
// @Tags 订单
// @Summary 根据id及jwt发货
// @Router /order/jwt/send/id [get]
func DeliverOrderByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	orderGet, _ := OrderService.GetOrderById(id.(int))
	shopId := orderGet.ShopId
	shop, _ := ShopService.GetShopById(shopId)
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备支付该订单权限", Data: nil})
	}
	now := time.Now()
	timeNow := now.Format("2006-01-02 15:04:05")
	order := models.Order{
		ID:       id.(int),
		SendTime: timeNow,
	}
	OrderService.UpdateOrder(order)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及jwt发货成功", Data: nil})
}
